﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Table_Dal;
using System.IO;
namespace BLL
{
    public class BusinessLogicLayer
    {

        public string RandomMa()
        {
            Random rd = new Random();
            int x = rd.Next(100, 1000000);
            return x.ToString();
        }
        public int RandomIndex(int size)
        {
            Random rd = new Random();
            int x = rd.Next(0, size - 1);

            return x;
        }

        public List<CauhoiThi> TronDanhSachCauHoi(List<CauhoiThi> Danhsach)
        {
            
            List<CauhoiThi> DanhsachMoi = new List<CauhoiThi>();
            List<CauhoiThi> DanhsachTemp = new List<CauhoiThi>();
            foreach (CauhoiThi i in Danhsach)
            {
                CauhoiThi ch = new CauhoiThi();
                ch.Flag = i.Flag;
                ch.HinhAnh = i.HinhAnh;
                ch.MaCauHoi = i.MaCauHoi;
                ch.NoiDung = i.NoiDung;
                ch.SoDapAnDung = i.SoDapAnDung;
                DanhsachTemp.Add(ch);
            }
            int count = DanhsachTemp.Count;
            while (count > 0)
            {
                int temp = RandomIndex(count);
                DanhsachMoi.Add(DanhsachTemp[temp]);
                DanhsachTemp.RemoveAt(temp);
                count--;
            }
            return DanhsachMoi;
        }
        public List<TraloiThi> TronDanhSachDapAn(List<TraloiThi> Danhsach)
        {
            List<TraloiThi> DanhsachTemp = new List<TraloiThi>();
            foreach (TraloiThi i in Danhsach)
            {
                TraloiThi temp = new TraloiThi();
                temp.LoaiDA = i.LoaiDA;
                temp.NoiDung = i.NoiDung;
                temp.MaDapAn = i.MaDapAn;
                temp.HinhAnh = i.HinhAnh;
                temp.MaCauHoi = i.MaCauHoi;
                DanhsachTemp.Add(temp);
            }
            int count = DanhsachTemp.Count;
            List<TraloiThi> DanhsachMoi = new List<TraloiThi>();
            while (count > 0)
            {
                int temp = RandomIndex(count);
                DanhsachMoi.Add(DanhsachTemp[temp]);
                DanhsachTemp.RemoveAt(temp);
                count--;
            }
            return DanhsachMoi;
        }
        public List<TraloiThi> DanhsachDapAn_TheoListCauHoi(List<CauhoiThi> Dscauhoi)
        {
            DapAnDal dadal = new DapAnDal();
            List<TraloiThi> DsDapAnMoi = new List<TraloiThi>();
            List<TraloiThi> DsDapAnChuaTron;
            List<TraloiThi> DsDapAnTron;
            foreach (CauhoiThi ch in Dscauhoi)
            {
                DsDapAnChuaTron = new List<TraloiThi>();
                DsDapAnChuaTron = dadal.DanhsachDapAn_Thi(ch.MaCauHoi);
                DsDapAnTron = new List<TraloiThi>();
                DsDapAnTron = this.TronDanhSachDapAn(DsDapAnChuaTron);
                foreach (TraloiThi da in DsDapAnTron)
                {
                    DsDapAnMoi.Add(da);
                }
            }
            return DsDapAnMoi;
        }
        public string CopyPicture(string LinkCopy, string Folder, string Filename)
        {

            string Duoi = LinkCopy.Substring(LinkCopy.Length - 4);
            string LinkSave = @"HINHANH\" + Folder + @"\" + Filename + Duoi;
            try
            {
                File.Copy(LinkCopy, LinkSave, true);
            }
            catch
            {

            }

            return LinkSave;

        }
        public bool XoaPicture(string Link)
        {
            File.Delete(Link);
            return true;
        }
        public bool KiemTraChuoi(string str1, string str2)
        {
            if (str1 == str2)
                return true;
            return false;
        }
        
    }
}
